<?php
/**
 * xushuhui
 * 2017/8/4
 * 17:22
 */

namespace app\index\controller;

// 用户中心
use app\index\model\Card as CardModel;
use app\index\model\Order;
use app\index\model\Recharge;
use app\index\model\User;
use app\index\service\WechatPay;
use app\lib\exception\PasswordException;
use app\lib\exception\SuccessMessage;
use app\lib\validate\IDMustBePostiveInt;
use app\lib\validate\PasswordValidate;
use app\lib\validate\RechargeValidate;
use think\Request;


class My extends Base
{
	//个人中心
	public function index()
	{

		return view();
	}

	//支付页面
	public function pay($id)
	{
		(new IDMustBePostiveInt())->goCheck();
		$orderObj = Order::with(['plan','wxuser'])->find($id);
		$order = $orderObj->toArray();
		$notifyUrl = url('Index/Pay/notify','',true,true);
		$jsApiParameters = WechatPay::makeWxPreOrder($order,$order['wxuser']['openid'],$notifyUrl);
		$this->assign('data',$order);
		$this->assign('jsApiParameters',$jsApiParameters);
		return view();
	}
	// 充值
	public function recharge()
	{
		$data = Request::instance()->post();
		if ($data) {
			(new RechargeValidate())->goCheck();
			$data['orderNo'] = getOrderNo();
			$res = Recharge::create($data);
			$url = url('dopay',['id'=>$res->id]);
			throw new SuccessMessage(['data' => $url]);
		}else{
			$usermoney = User::get($this->uid)['money'];
			$this->assign('usermoney',$usermoney);
			return view();
		}
	}
	// 支付
	public function dopay($id)
	{
		(new IDMustBePostiveInt())->goCheck();
		$orders = Order::get($id);
	
		$notifyUrl = url('Index/Pay/notify','',true,true);
	
		$jsApiParameters = WechatPay::makeWxPreOrder($orders,$this->openid,$notifyUrl);
		
		$this->assign('jsApiParameters',$jsApiParameters);
		return view();
	}
	// 修改密码
	public function password()
	{
		$data = Request::instance()->post();
		if ($data) {
			(new PasswordValidate())->goCheck();
			$userData = User::get($this->uid);
			if(md5($data['oldPassword']) !== $userData['password']){
				throw new PasswordException([
					'msg' => '旧密码输入错误'
				]);
			}
			$res = User::update(['password' => $data['newPassword']],['id' => $this->uid]);
			if($res){
				throw new SuccessMessage(['msg' => '密码修改成功']);
			}
		} else{
			return view();
		}
	}
	// 操作手册
	public function operation()
	{
		return view();
	}
	// 提现
	public function withdraw()
	{
		return view();
	}

}